package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.maker.MakerBalanceFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.project.CalcAmountType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.core.service.CoreInvoiceAmountService;
import com.info.zhiduoduo.core.service.CoreProjectService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.maker.MakerBalanceDto;
import com.info.zhiduoduo.repository.mapper.maker.MakerStudioMapper;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

/**
 * 创客可用额度查询
 */

@Service
public class ProjectBalanceService extends BaseService {

    @Resource
    MakerStudioMapper makerStudioMapper;
    @Resource
    CoreProjectService coreProjectService;
    @Resource
    CoreInvoiceAmountService coreInvoiceAmountService;

    public SmartResult queryMakerAllProjectAmount(BaseSimpleFormBean<MakerBalanceFormBean> formBean){
        StringBuilder str = new StringBuilder();
        String name = formBean.getReqdata().getName();
        String compname = formBean.getReqdata().getCompname();
        String openingLocation = formBean.getReqdata().getOpeningLocation();
        str.append("1=1");
        if (!StringUtil.isEmpty(name)) {
            str.append(" and ( a.realname like '%").append(name).append("%'")
                    .append(" or s.username like '%").append(name).append("%' )");
        }
        if (!StringUtil.isEmpty(compname)) {
            str.append(" and compname like '%").append(compname).append("%'");
        }
        if(!StringUtil.isEmpty(openingLocation)){
            str.append(" and zcc.name =").append(openingLocation);
        }

        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<MakerBalanceDto> makerBalanceDto = makerStudioMapper.queryAllMakerStudio(str);
        makerBalanceDto.stream().forEach(makerBalance->{
            BigDecimal monthlyTotalAmount = coreInvoiceAmountService.getUserMonthlyTotalAmount(makerBalance.getUserid());
            BigDecimal quarterTotalAmount = coreInvoiceAmountService.getUserQuarterTotalAmount(makerBalance.getUserid());
            BigDecimal yearlyTotalAmount = coreInvoiceAmountService.getUserYearlyTotalAmount(makerBalance.getUserid());
            BigDecimal twelveMonthTotalAmount = coreInvoiceAmountService.getUserTwelveMonthTotalAmount(makerBalance.getUserid());
            makerBalance.setInvoicingAmount(coreInvoiceAmountService.getUserInvoicingAmount(makerBalance.getUserid()));
            makerBalance.setQuarterStrategyAmount(coreInvoiceAmountService.getUserQuarterStrategyInvoicingAmount(makerBalance.getUserid()));
            makerBalance.setMonthBusiness(monthlyTotalAmount);
            makerBalance.setQuarterBusiness(quarterTotalAmount);
            makerBalance.setYearBusiness(yearlyTotalAmount);
            makerBalance.setTwelveMonthAmount(twelveMonthTotalAmount);
        });
        return smartResult(makerBalanceDto, page.getTotal(), page.getPages(), page.getPageNum());
    }

    public SmartResult queryBalanceCompanyCompany(BaseSimpleFormBean<MakerBalanceFormBean> formBean) {
        String str = addCondition(formBean);
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<MakerBalanceDto> makerBalanceDto = makerStudioMapper.queryBalanceCompany(str);
        return smartResult(makerBalanceDto, page.getTotal(), page.getPages(), page.getPageNum());

    }

    public SmartResult queryBalanceCrowdsoucre(BaseSimpleFormBean<MakerBalanceFormBean> formBean) {
        String str = addCondition(formBean);
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<MakerBalanceDto> makerBalanceDto = makerStudioMapper.queryBalanceCrowdsoucre(str);
        return smartResult(makerBalanceDto, page.getTotal(), page.getPages(), page.getPageNum());

    }

    public SmartResult queryBalanceTaxerInvoice(BaseSimpleFormBean<MakerBalanceFormBean> formBean) {
        String str = addCondition(formBean);
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<MakerBalanceDto> makerBalanceDto = makerStudioMapper.queryBalanceTaxerInvoice(str);
        return smartResult(makerBalanceDto, page.getTotal(), page.getPages(), page.getPageNum());

    }
     //添加筛选条件
    public String addCondition(BaseSimpleFormBean<MakerBalanceFormBean> formBean) {
        StringBuilder stringBuilder = new StringBuilder();
        String name = formBean.getReqdata().getName();
        String compname = formBean.getReqdata().getCompname();
        String openingLocation = formBean.getReqdata().getOpeningLocation();
        stringBuilder.append("1=1");
        if (!StringUtil.isEmpty(name)) {
            stringBuilder.append(" and ( realname like '%").append(name).append("%'")
                    .append(" or stu.username like '%").append(name).append("%' )");
        }
        if (!StringUtil.isEmpty(compname)) {
            stringBuilder.append(" and compname like '%").append(compname).append("%'");
        }
        if(!StringUtil.isEmpty(openingLocation)){
            stringBuilder.append(" and zcc.name =").append(openingLocation);
        }
        return stringBuilder.toString();
    }

     //添加筛选条件
    public String addConditionALL(BaseSimpleFormBean<MakerBalanceFormBean> formBean) {
        StringBuilder stringBuilder = new StringBuilder();
        String name = formBean.getReqdata().getName();
        String compname = formBean.getReqdata().getCompname();
        stringBuilder.append("1=1");
        if (!StringUtil.isEmpty(name)) {
            stringBuilder.append(" and ( tmp.realname like '%").append(name).append("%'")
                    .append(" or tmp.username like '%").append(name).append("%' )");
        }
        if (!StringUtil.isEmpty(compname)) {
            stringBuilder.append(" and tmp.compname like '%").append(compname).append("%'");
        }
        return stringBuilder.toString();
    }
}
